package org.pinda.gui;

import java.util.LinkedList;

import org.johnnei.engine.render.ColorHelper;

/**
 * A textbox which only accepts certain digits as typable characters
 * @author Johnnei
 *
 */
public class GuiFilteredTextBox extends GuiTextBox {

	private LinkedList<Integer> acceptableCharacters;
	
	public GuiFilteredTextBox(Gui parent, String defaultText, float x, float y, float width, float height, ColorHelper backgroundColor, ColorHelper textColor) {
		super(parent, defaultText, x, y, width, height, backgroundColor, textColor);
		acceptableCharacters = new LinkedList<>();
		addAcceptable(0);
		addAcceptable(8);
	}
	
	/**
	 * Adds a single character to the acceptable characters
	 * @param c
	 */
	public void addAcceptable(char i) {
		acceptableCharacters.add((int)i);
	}
	
	/**
	 * Adds a single character to the acceptable characters
	 * @param c
	 */
	public void addAcceptable(int i) {
		acceptableCharacters.add(i);
	}
	
	/**
	 * Adds a range to the acceptable characters
	 * @param start start digit (included)
	 * @param end end digit (included)
	 */
	public void addAcceptableRange(int start, int end) {
		for(int i = start; i <= end; i++) {
			acceptableCharacters.add(i);
		}
	}
	
	@Override
	public void onKeyPress(char c) {
		if(hasFocus()) {
			for(int i : acceptableCharacters) {
				if(i == (int)c) {
					super.onKeyPress(c);
					return;
				}
			}
		}
	}
}
